package com.neusoft.nep.GridOfficer.controller;


import com.neusoft.nep.common.FileUtils;
import com.neusoft.nep.entity.Feedback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TableCell;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

public class GirdOfficerFeedBackListViewController {
    static public String index;
    //当列表内容变化时，表格会自动刷新显示
    public ObservableList<Feedback> tableList = FXCollections.observableArrayList();
    @FXML
    private TableView<Feedback> tableView;
    @FXML
    private TableColumn<Feedback, String> colLevel;
    @FXML
    private TableColumn<Feedback, String> colProvince;
    @FXML
    private TableColumn<Feedback, String> colCity;
    @FXML
    private TableColumn<Feedback, String> colAddr;
    @FXML
    private TableColumn<Feedback, String> colTime;
    @FXML
    private TableColumn<Feedback, Void> colAssign;
    private Map dateMap = new HashMap<>();

    //获得当前反馈数据的序号
    public static String getIndex() {
        return index;
    }

    public static void delInfo(String index) {
        String fileName = GirdOfficerLoginViewController.getOfficerName();
        //读取由系统管理员传来的待检测文件
        Map dateMap = new HashMap<>();
        String fileName1 = GirdOfficerLoginViewController.getOfficerName();
        dateMap = FileUtils.readMapObject(fileName + ".txt");
        dateMap.remove(index);
        FileUtils.writeMapObject(fileName1 + ".txt", dateMap);
    }

    @FXML
    public void initialize() throws IOException {
        colLevel.setCellValueFactory(new PropertyValueFactory<>("aqiLevel"));
        colProvince.setCellValueFactory(new PropertyValueFactory<>("province"));
        colCity.setCellValueFactory(new PropertyValueFactory<>("city"));
        colAddr.setCellValueFactory(new PropertyValueFactory<>("address"));
        colTime.setCellValueFactory(new PropertyValueFactory<>("time"));
        //从文件中读取所有对象数据
        String fileName = GirdOfficerLoginViewController.getOfficerName();
        dateMap = FileUtils.readMapObject(fileName + ".txt");
        //在列表中展示所有上方要显示的数据
        tableList.addAll(dateMap.values());
        tableView.setItems(tableList);
        //动态添加按钮列
        colAssign.setCellFactory(cols -> new TableCell<>() {
            private Button btn = new Button("去检测");

            {
                btn.setOnAction(e -> {
                    Feedback date = getTableView().getItems().get(getIndex());
                    System.out.println("点击了指派：" + date.getAddress() + "可以弹出窗口进行其他操作");
                    Stage stage = new Stage();
                    FXMLLoader fxmlLoader = new FXMLLoader();
                    URL url = getClass().getResource("/com/neusoft/nep/GridOfficer/view/GirdOfficerFeedBackInfoView.fxml");
                    fxmlLoader.setLocation(url);
                    Parent root = null;
                    try {
                        root = fxmlLoader.load();
                    } catch (IOException ex) {
                        throw new RuntimeException(ex);
                    }
                    // 获取详情页面控制器
                    GirdOfficerFeedBackInfoViewController infoController = fxmlLoader.getController();
                    // 将点击行的 Date 对象传递给详情页面控制器
                    infoController.setData(date);
                    //获得当前反馈数据的序号
                    index = String.valueOf(date.getIndex());
                    //展示出窗口
                    Scene scene = btn.getScene();
                    scene.setRoot(root);
                    stage = (Stage) scene.getWindow();
                    stage.show();
                });
            }

            @Override
            protected void updateItem(Void item, boolean empty) {
                super.updateItem(item, empty);
                setGraphic(empty ? null : btn);
            }
        });

    }


}

